<!DOCTYPE HTML>
<html lang="en">
<head>
<title>WinExist() / IfWinExist - Syntax &amp; Usage | AutoHotkey</title>
<meta name="description" content="The WinExist function and IfWinExist statement check if a matching window exists." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>WinExist() / IfWin[Not]Exist</h1>
<p>Checks if a matching window exists. If it is, <em>WinExist()</em> returns the Unique ID (HWND) of the first matching window.</p>

<h2 id="function">WinExist()</h2>
<pre class="Syntax">UniqueID := <span class="func">WinExist</span>(WinTitle <span class="optional">, WinText, ExcludeTitle, ExcludeText</span>)</pre>

<h3>Parameters</h3>
<dl>

  <dt>WinTitle</dt>
  <dd><p>A window title or other criteria identifying the target window. See <a href="../misc/WinTitle.htm">WinTitle</a>.</p></dd>

  <dt>WinText</dt>
  <dd><p>If present, this parameter must be a substring from a single text element of the target window (as revealed by the included Window Spy utility). Hidden text elements are detected if <a href="DetectHiddenText.htm">DetectHiddenText</a> is ON.</p></dd>

  <dt>ExcludeTitle</dt>
  <dd><p>Windows whose titles include this value will not be considered.</p></dd>

  <dt>ExcludeText</dt>
  <dd><p>Windows whose text include this value will not be considered.</p></dd>

</dl>

<h3>Return Value</h3>
<p>This function returns the <a href="../misc/WinTitle.htm#ahk_id">Unique ID (HWND)</a> (as hexadecimal integer) of the first matching window (0 if none). Since all non-zero numbers are seen as &quot;true&quot;, the statement <code>if WinExist(WinTitle)</code> is true whenever <em>WinTitle</em> exists.</p>

<h3>Examples</h3>
<pre class="NoIndent">if WinExist(&quot;ahk_class Notepad&quot;) or WinExist(&quot;ahk_class&quot; . ClassName)
    WinActivate  <em>; Uses the <a href="../misc/WinTitle.htm#LastFoundWindow">last found window</a>.</em>

MsgBox % &quot;The active window's ID is &quot; . WinExist(&quot;A&quot;)</pre>
  <pre class="NoIndent">If !WinExist(&quot;Calculator&quot;)  <em>; Equivalent to IfWinNotExist, Calculator</em>
    return</pre>
	
<h2 id="command">IfWin[Not]Exist</h2>
<p class="warning"><strong>Deprecated:</strong> This command is not recommended for use in new scripts. Use the <a href="#function">WinExist</a> function instead.</p>
<pre class="Syntax">
<span class="func">IfWinExist</span> <span class="optional">, WinTitle, WinText, ExcludeTitle, ExcludeText</span>
<span class="func">IfWinNotExist</span> <span class="optional">, WinTitle, WinText, ExcludeTitle, ExcludeText</span>
</pre>

<h3>Parameters</h3>
<dl>

  <dt>WinTitle</dt>
  <dd><p>A window title or other criteria identifying the target window. See <a href="../misc/WinTitle.htm">WinTitle</a>.</p></dd>

  <dt>WinText</dt>
  <dd><p>If present, this parameter must be a substring from a single text element of the target window (as revealed by the included Window Spy utility). Hidden text elements are detected if <a href="DetectHiddenText.htm">DetectHiddenText</a> is ON.</p></dd>

  <dt>ExcludeTitle</dt>
  <dd><p>Windows whose titles include this value will not be considered.</p>
      <p class="note"><strong>Note</strong>: Due to backward compatibility, <em>IfWin[Not]Exist</em> interpret this parameter as a command if it exactly matches the name of a command. To work around this, use the <a href="#function">WinExist</a> function instead.</p></dd>

  <dt>ExcludeText</dt>
  <dd><p>Windows whose text include this value will not be considered.</p></dd>

</dl>

<h3>Remarks</h3>
<p><a href="SetWinDelay.htm">SetWinDelay</a> does not apply to this command.</p>

<h3>Examples</h3>
<pre class="NoIndent">IfWinExist, Untitled - Notepad
{
    WinActivate  <em>; Automatically uses the window found above.</em>
    WinMaximize  <em>; same</em>
    Send, Some text.{Enter}
    return
}</pre>
<pre class="NoIndent">IfWinNotExist, Calculator
    return
else
{
    WinActivate  <em>; The above &quot;IfWinNotExist&quot; also set the &quot;last found&quot; window for us.</em>
    WinMove, 40, 40  <em>; Move it to a new position.</em>
    return
}</pre>

<h2>Remarks</h2>
<p>If all parameters are omitted, the <a href="../misc/WinTitle.htm#LastFoundWindow">Last Found Window</a> will be checked to see if it still exists (or doesn't exist in the case of <i>IfWinNotExist</i>).</p>
<p>If the function or command determines that a qualified window exists, the <a href="../misc/WinTitle.htm#LastFoundWindow">Last Found Window</a> will be updated to be that window. In other words, if <em>WinExist</em> or <em>IfWinExist</em> evaluates to true or <em>IfWinNotExist</em> evaluates to false, the <a href="../misc/WinTitle.htm#LastFoundWindow">Last Found Window</a> will be updated.</p>
<p>To discover the HWND of a control (for use with <a href="PostMessage.htm">Post/SendMessage</a> or <a href="DllCall.htm">DllCall</a>), use <a href="ControlGet.htm#Hwnd">ControlGet Hwnd</a> or <a href="MouseGetPos.htm">MouseGetPos</a>.</p>
<p>Window titles and text are case sensitive. Hidden windows are not detected unless <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> has been turned on.</p>

<h2>Related</h2>
<p><a href="WinActive.htm">WinActive() / IfWinActive</a>, <a href="SetTitleMatchMode.htm">SetTitleMatchMode</a>, <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a>, <a href="../misc/WinTitle.htm#LastFoundWindow">Last Found Window</a>, <a href="Process.htm">Process</a>, <a href="WinActivate.htm">WinActivate</a>, <a href="WinWaitActive.htm">WinWaitActive</a>, <a href="WinWait.htm">WinWait</a>, <a href="WinWaitClose.htm">WinWaitClose</a>, <a href="_IfWinActive.htm">#IfWinActive/Exist</a></p>
	
</body>
</html>
